<template>
  <div class="bottom-bar">
    <div class="check-content">
      <checkButton :is-checked="isSelectAll" class="check-button" @click="checkClick"/>
      <span>全选</span>
    </div>
    <div class="price">
      合计: {{totalPrice}}
    </div>
    <div class="calculate" @click="calcClick">
      去计算({{checkLength}})
    </div>
  </div>
</template>

<script>
import checkButton from 'components/content/check_button/check_button'

import { mapGetters } from 'vuex';

export default {
  name: "cart_bottom_bar",
  components: {
    checkButton
  },
  computed: {
    ...mapGetters(['cartList']),
    totalPrice() {
      return '¥' + this.cartList.filter(item => {
        // 第一层过滤, 判断是否选中
        return item.checked;
      }).reduce((preValue, item) => {
        return preValue + parseFloat(item.oldPrice.replace('¥','') * item.count);
      }, 0).toFixed(2)
    },
    checkLength() {
      return this.cartList.filter(item => {
        // 第一层过滤, 判断是否选中
        return item.checked;
      }).reduce((preValue, item) => {
        return preValue + item.count;
      }, 0)
    },
    isSelectAll() {
      if(this.cartList.length === 0) return false;
      else return !this.cartList.find(item => !item.checked);
    }
  },
  methods: {
    checkClick() {
      if(this.isSelectAll) {
        this.cartList.forEach(item => item.checked = false);
      } else {
        this.cartList.forEach(item => item.checked = true);
      }
    },
    calcClick() {
      if(!this.isSelectAll) {
        this.$TOAST.show('请选择购买的商品', 1500)
      }
    }
  }
}
</script>

<style scoped>
  .bottom-bar {
    height: 40px;
    line-height: 40px;
    position: relative;
    display: flex;
    background-color: #eee;
    font-size: 15px;
  }

  .check-content {
    width: 60px;
    display: flex;
    align-items: center;
    margin-left: 10px;
  }

  .check-button {
    width: 20px;
    height: 20px;
    line-height: 20px;
    margin-right: 4px;
  }

  .price {
    margin-left: 20px;
    flex: 1;
  }

  .calculate {
    width: 90px;
    background-color: red;
    color: fff;
    text-align: center;
  }
</style>